/*
 * 项目名称:platform-plus
 * 类名称:ExpenseController.java
 * 包名称:com.platform.modules.custmoer.expense.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-10-10 15:52:59        李鹏军     初版做成
 *
 * Copyright (c) 2019-2019 klwu
 */
package com.platform.modules.custmoer.controler;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.platform.common.annotation.SysLog;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.RestResponse;
import com.platform.modules.custmoer.dto.OrderDetailDto;
import com.platform.modules.custmoer.entity.ExpenseEntity;
import com.platform.modules.custmoer.service.ExpenseService;
import com.platform.modules.custmoer.service.StaffService;
import com.platform.modules.httpclient.HttpClient;
import com.platform.modules.httpclient.HttpMethod;
import com.platform.modules.httpclient.HttpParamers;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Controller
 *
 * @author
 * @date 2020-10-10 15:52:59
 */
@RestController
@RequestMapping("expense/")
public class ExpenseController extends AbstractController {
    @Autowired
    private ExpenseService expenseService;
    @Autowired
    private StaffService staffService;


    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("expense::list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<ExpenseEntity> list = expenseService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("expense::list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = expenseService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("expense::info")
    public RestResponse info(@PathVariable("id") String id) {
        ExpenseEntity expense = expenseService.getById(id);
        if(Objects.nonNull(expense)){
            expense.setStaffName(staffService.getById(expense.getStaffId()).getName());
            if(expense.getStatus().equals(1)){
                expense.setStatusStr("待审核");
            }else if(expense.getStatus().equals(2)){
                expense.setStatusStr("已完成");
            }else if(expense.getStatus().equals(0)){
                expense.setStatusStr("不通过");
            }else if(expense.getStatus().equals(3)){
                expense.setStatusStr("已撤销");
            }
        }
        return RestResponse.success().put("data", expense);
    }

    /**
     * 新增
     *
     * @param expense expense
     * @return RestResponse
     */
    @SysLog("新增")
    @RequestMapping("/save")
    @RequiresPermissions("expense::save")
    public RestResponse save(@RequestBody ExpenseEntity expense) {

        expenseService.add(expense);

        return RestResponse.success();
    }

    /**
     * 修改
     *
     * @param expense expense
     * @return RestResponse
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("expense::update")
    public RestResponse update(@RequestBody ExpenseEntity expense) {

        expenseService.update(expense);

        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("expense::delete")
    public RestResponse delete(@RequestBody String[] ids) {
        expenseService.deleteBatch(ids);

        return RestResponse.success();
    }

    @GetMapping("/check")
    public RestResponse check(@RequestParam Map<String, Object> params) {
        ExpenseEntity expenseEntity = expenseService.getById(params.get("id").toString());
        if(null != expenseEntity){
            Integer status = Integer.parseInt(params.get("status").toString());
            Object comment = params.get("comment");
            expenseEntity.setStatus(status);
            if(comment != null && comment != ""){
                expenseEntity.setCheckComment(comment.toString());
            }
            expenseService.update(expenseEntity);
        }
        return RestResponse.success().put("msg", "success");
    }

    @RequestMapping("/order/detail/{code}")
    public RestResponse orderDetail(@PathVariable String code) throws IOException {
        OrderDetailDto orderDetailDto = new OrderDetailDto();
        String url = "http://verify.mengtelaidi.com/api/getOrdersDetailByPhone?token=eyJhbGciOiJIUzI1N&orderNumber="+code;
        HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
        String result = HttpClient.doGet(url, httpParamers, null, 6000, 6000);
        System.out.println(result);
        JsonObject jsonObj = JsonUtils.getJsonObj(result);
        if(jsonObj.get("code").getAsInt() != 200){
            throw new BusinessException("调用外部接口返回编码="+jsonObj.get("code").getAsInt());
        }
        JsonElement data = jsonObj.get("data");
        JsonArray jsonArray = null;
        if(data.getAsJsonArray().size() != 0){
            jsonArray = data.getAsJsonArray();
            JsonElement jsonElement = jsonArray.get(0);
            String s = jsonElement.toString();
            JsonObject order = JsonUtils.getJsonObj(s);
            String orderNumber = order.get("orderNumber").getAsString();
            orderDetailDto.setCode(orderNumber);
            JsonElement orderState = order.get("orderState");
            JsonElement consigneeName = order.get("consigneeName");
            JsonElement settlementAmount = order.get("settlementAmount");
            JsonElement payMethod = order.get("payMethod");
            JsonElement orderType = order.get("orderType");
            JsonElement consigneePhone = order.get("consigneePhone");
            JsonElement addressDetail = order.get("addressDetail");
            JsonElement createTime = order.get("createTime");
            JsonElement remark = order.get("remark");
            if(!orderState.isJsonNull()){
                orderDetailDto.setOrderState(orderState.getAsString());
            }
            if(!consigneeName.isJsonNull()){
                orderDetailDto.setConsigneeName(consigneeName.getAsString());
            }
            if(!settlementAmount.isJsonNull()){
                float asFloat = settlementAmount.getAsFloat();
                orderDetailDto.setAmount(String.valueOf(asFloat));
            }
            if(!payMethod.isJsonNull()){
                orderDetailDto.setPayMethod(payMethod.getAsString());
            }
            if(!orderType.isJsonNull()){
                orderDetailDto.setOrderType(orderType.getAsString());
            }
            if(!consigneePhone.isJsonNull()){
                orderDetailDto.setConsigneePhone(consigneePhone.getAsString());
            }
            if(!addressDetail.isJsonNull()){
                orderDetailDto.setAddressDetail(addressDetail.getAsString());
            }
            if(!remark.isJsonNull()){
                orderDetailDto.setRemark(remark.getAsString());
            }
            if(!createTime.isJsonNull()){
                orderDetailDto.setCreateDateStr(createTime.getAsString());
            }
        }

        return RestResponse.success().put("order",orderDetailDto);
    }

}
